package com.zenithmind.common.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

public class FileUtils {
    private final static String FILE_PATH = System.getProperty("user.dir") + "/files/";
    private final static String FILE_URL = "http://localhost:8080/files/";

    // 文件上传
    public static String upload(MultipartFile file) {
        try {
            String fileName = file.getOriginalFilename();
            if (fileName.length() > 0) {
                String filePath = FILE_PATH + fileName;
                file.transferTo(new File(filePath));
                return FILE_URL + fileName;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}
